package com.hellobike.haxing.web.order;

import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.order.api.IOrderAftersaleReasonService;
import com.hellobike.haxing.order.api.IOrderAftersaleService;
import com.hellobike.haxing.order.api.IOrderBService;
import com.hellobike.haxing.order.api.IWmsDeliveryService;
import com.hellobike.haxing.order.model.request.*;
import com.hellobike.haxing.order.model.vo.*;
import com.hellobike.haxing.order.model.wms.DeliveryOrderHeaderModel;
import com.hellobike.haxing.order.result.PageResult;
import com.hellobike.haxing.order.result.Result;
import com.hellobike.haxing.request.PageRequest;
import com.hellobike.haxing.request.order.OrderDeliveryRequest;
import com.hellobike.haxing.user.vo.UserVo;
import com.hellobike.haxing.util.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

@Api(tags = "订单管理")
@RestController
@RequestMapping(value = "/orderB")
@Slf4j
public class OrderController {
    @Reference(version = "1.0.0")
    private IOrderBService orderBService;
    @Reference(version = "1.0.0")
    private IOrderAftersaleService orderAftersaleService;
    @Reference(version = "1.0.0")
    private IWmsDeliveryService wmsDeliveryService;
    @Reference(version = "1.0.0")
    private IOrderAftersaleReasonService orderAftersaleReasonService;
    @Autowired
    private UserUtil userUtil;

    @PostMapping(value = "/queryOrderBPage")
    @ApiOperation("全部订单")
    public PageResult<OrderBVo> queryOrderBPage(@Valid @ApiParam("全部订单分页查询") @RequestBody OrderBRequestPage res) throws Exception {
        res.setShopId(userUtil.getUser().getShopVo().getId());
        PageResult<OrderBVo> result = orderBService.queryOrderBPage(res);
        return result;
    }

    @PostMapping(value = "/delivery")
    @ApiOperation("订单交货")
    public Result<OrderDeliveryVo> delivery(@Valid @ApiParam(name = "订单交货请求", required = true) @RequestBody OrderDeliveryRequest res) throws Exception {
        Result<OrderDeliveryVo> result = orderBService.delivery(res.getOrderId(), res.getDeliveryCode());
        return result;
    }


    /**
     * 订单详情
     */
    @GetMapping(value = "/getOrderBInfo")
    @ApiOperation("订单详情")
    public Result<OrderBInfoVo> getOrderBInfo(@Valid @NotNull(message = "orderId不为空") @ApiParam(name = "orderId", required = true) @RequestParam Long orderId) throws Exception {
        Result<OrderBInfoVo> result = orderBService.getOrderBInfo(orderId);
        return result;
    }

    /**
     * 待揽收列表
     */
    @GetMapping(value = "/queryWaitReceived")
    @ApiOperation("待揽收列表")
    public Result<List<DeliveryOrderHeaderModel>> queryWaitReceived() throws Exception {

        Result<List<DeliveryOrderHeaderModel>> result = wmsDeliveryService.queryWaitReceivedDeliveryOrderDetails(userUtil.getUser().getShopVo().getId());
        log.info("delivery-order222:{}", JSON.toJSONString(result));
        return result;
    }

    /**
     * 确认揽收
     */
    @PostMapping(value = "/comfireReceived")
    @ApiOperation("确认揽收")
    public Result<Boolean> comfireReceived(@Valid @RequestBody WmsDeliveryOrderRequest res) throws Exception {
        UserVo userVo = userUtil.getUser();
        return orderBService.comfireReceived(res, userVo.getShopVo().getId(), userVo.getId());
    }

    /**
     * 揽收异常原因列表
     */
    @RequestMapping(value = "/getReceivedExtList", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation("揽收异常原因列表")
    public Result<List<String>> getReceivedExtList() throws Exception {
        Result<List<String>> result = wmsDeliveryService.getReceivedExtList();
        return result;
    }

    /**
     * 售后分页查询
     */
    @PostMapping(value = "/queryAftersalePage")
    @ApiOperation("售后订单分页")
    public PageResult<OrderAftersaleListVo> queryAftersalePage(@Valid @ApiParam("售后订单分页") @RequestBody OrderAftersaleRequestPage res) throws Exception {
        res.setShopId(userUtil.getUser().getShopVo().getId());
        PageResult<OrderAftersaleListVo> result = orderAftersaleService.queryAftersalePage(res);
        return result;
    }

    /**
     * 今日订单
     */
    @PostMapping(value = "/day-orders")
    @ApiOperation("今日订单")
    public PageResult<OrderBVo> dayOrders(@RequestBody PageRequest request) {
        OrderBRequestPage requestPage = new OrderBRequestPage();
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, 0);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            requestPage.setStartTime(calendar.getTime());
            requestPage.setEndTime(DateUtils.addDays(requestPage.getStartTime(), 1));
            requestPage.setPayStatus(1);
            requestPage.setCurPage(request.getCurPage());
            requestPage.setPageSize(request.getPageSize());
            requestPage.setShopId(userUtil.getUser().getShopVo().getId());
            PageResult<OrderBVo> result = orderBService.queryOrderBPage(requestPage);
            return result;
        } catch (Exception ex) {
            PageResult<OrderBVo> result = new PageResult<OrderBVo>();
            result.faild(500, ex.getMessage());
            return result;
        }
    }

    /**
     * 售后申请
     */
    @PostMapping(value = "/applyAftersale")
    @ApiOperation("售后申请")
    public Result<OrderAftersaleApplySuccessVo> applyAftersale(@Valid @ApiParam(value = "售后订单申请请求", required = true) @RequestBody OrderAftersaleRequest res) throws Exception {
        Result<OrderAftersaleApplySuccessVo> result = orderAftersaleService.applyAftersale(res, userUtil.getUser().getShopVo().getId());
        return result;
    }

    /**
     * 售后申请查询
     */
    @GetMapping(value = "/getAfterSaleApply")
    @ApiOperation("售后申请查询")
    public Result<OrderAftersaleApplyVo> getAfterSaleApply(@Valid @NotNull(message = "orderId不为空") @ApiParam(name = "orderId", required = true) @RequestParam Long orderId) throws Exception {
        Result<OrderAftersaleApplyVo> result = orderAftersaleService.getAfterSaleApply(orderId);
        return result;
    }

    /**
     * 售后订单退款金额计算
     */
    @PostMapping(value = "/getRefundAmount")
    @ApiOperation("售后订单退款金额计算")
    public Result<BigDecimal> applyAftersale(@Valid @ApiParam("售后订单退款金额计算") @RequestBody OrderAfterReturnAmountRequest res) throws Exception {
        Result<BigDecimal> result = orderAftersaleService.getRefundAmount(res);
        return result;
    }

    /**
     * 确认退回站点
     */
    @RequestMapping(value = "/confirmReturn", method = {RequestMethod.PUT, RequestMethod.GET})
    @ApiOperation("确认退回站点")
    public Result<String> confirmReturn(@Valid @ApiParam(name = "确认退回站点", required = true) @RequestBody OrderAfterReturnRequest res) throws Exception {
        Result<String> result = orderAftersaleService.confirmReturn(res);
        return result;
    }

    /**
     * 确认回仓
     */
    @RequestMapping(value = "/confirmWarehouse", method = {RequestMethod.PUT, RequestMethod.GET})
    @ApiOperation("确认回仓")
    public Result<String> confirmWarehouse(@Valid @ApiParam(name = "确认回仓", required = true) @RequestBody OrderAfterReturnRequest res) throws Exception {
        Result<String> result = orderAftersaleService.confirmWarehouse(res);
        return result;
    }

    /**
     * B端售后详情
     */
    @GetMapping(value = "/getAfterSaleBInfo")
    @ApiOperation("B端售后详情")
    public Result<OrderAftersaleInfoListVo> getAfterSaleBInfo(@Valid @ApiParam(name = "afterId", required = true) @NotNull(message = "afterId不为空") @RequestParam Long afterId) throws Exception {
        Result<OrderAftersaleInfoListVo> result = orderAftersaleService.getAfterSaleBInfo(afterId);
        return result;
    }

}
